<?php
//FangCms核心数据查询类
namespace Org\Yike;
class FangCms{
    /**
     * 获取分类
     * @param string $model
     * @param int $key
     * $key值说明：
     *  1,组合一维数组
     *  2,组合多维数组
     *  3,传递一个子级返回父级id 例如:首页>>服装>>女装>>裙子
     *  4,传递一个父级ID返回所有子级分类
     *  5,获取指定父id的所有分类
     * @param array $data
     * @param string $field
     * @return array
     */
    static public function getCate($model='',$key=1,$data=array(),$field='id,name,alias,pid'){
        $list = M($model)->where(array('status'=>1))->order('ordid desc,id asc')->field($field)->select();
        $category = new Category();
        $listArr = array();
        if($key==1){
            $listArr = $category->unLimitedForLevel($list);
        }else if($key==2){
            $listArr = $category->umLimitedForLayer($list);
        }else if($key==3){
            $listArr = $category->getParents($list,$data['id']);
        }else if($key==4){
            $listArr = $category->getChiIds($list,$data['pid'],1);
        }else if($key==5){
            $listArr = $category->getCateByPid($list,$data['pid']);
        }
        return $listArr;
    }

    //获取分类下的所有文章
    static public function getIdsArticle($id,$limit=9,$field='*'){
        $where = array('status'=>1,'add_time'=>array('lt',time()));
        $category = new Category();
        $cate =  M('article')->where(array('status'=>1))->select();
        $ids = $category->getChiIds($cate,$id,1);
        array_push($ids,$id);
        $where['cate_id'] = array('in',$ids);
        $list = M('article')->where($where)->limit($limit)->field($field)->order('id desc')->select();
        return $list;
    }

    //获取一条分类
    static public function getCateOne($model='',$where = array('status'=>1),$field='*'){
        $info = M($model)->where($where)->field($field)->find();
        return $info;
    }

    //获取当前位置
    static public function currentLocation($model='',$where = array('status'=>1),$field='id,name,alias,pid'){
        $spid = M($model)->where($where)->field($field)->getField('spid');
        $spid = $spid ? explode(',',$spid) : array();
        array_unshift($spid,$where['id']);
        $list = M($model)->where(array('status'=>1,'id'=>array('in',implode(',',$spid))))->field($field)->select();
        return $list;
    }

    //增加浏览量
    static public function setInc($id,$model='ask',$where=''){
        if(!$where){
            $where = array('id'=>$id);
        }
        $h = cookie('hits');
        $hits_cookie = unserialize($h[$model]);
        if(($hits_cookie['id']==$id && ($hits_cookie['time']+8*3600) < time()) or $hits_cookie['id']!=$id){
            if(M($model)->where($where)->setInc('hits')){
                $hits = array();
                $hits[$model] = serialize(array('id'=>$id,'time'=>time()));
                cookie('hits',$hits);
            }
        }
    }

    //获取回答列表
    static public function getAnswerList($ask_id,$pageSize=5){
        $fex = C('DB_PREFIX');
        $answerObj = M("answer as a");
        $count = $answerObj->where(array('status'=>1,'ask_id'=>$ask_id))->count();
        $pager = new \Org\Util\Page($count, $pageSize);
        $list = $answerObj->join('left join '.$fex.'user as u on u.id = a.user_id')
            -> join('left join '.$fex.'user_info as ui on ui.user_id = u.id')
            -> field('a.*,u.username,u.id as user_id,ui.head_ico,ui.hot_line')
            -> where('a.status = 1 and ask_id = '.$ask_id)
            -> order('a.id desc')
            ->limit($pager->firstRow.','.$pager->listRows)
            -> select();
       return array('list'=>$list,'page'=>$pager->show(),'count'=>$count);
    }

    //获取问答列表
    static public function getAskList($pageSize=9,$where=array('a.status'=>1),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("ask as a")
                ->join('left join '.$fex.'ask_cate as ac on a.cate_id = ac.id')
                ->field('a.*,ac.alias,ac.name')
                ->where($where)
                ->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $askObj = M("ask as a");
        $askObj->join('left join '.$fex.'ask_cate as ac on a.cate_id = ac.id');
        $askObj->field('a.*,a.name as ask_name,ac.alias,ac.name');
        $askObj->where($where);
        $askObj->order('a.id desc');
        if($page){
            $askObj->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $askObj->limit($pageSize);
        }
        $arr['list'] = $askObj-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取用户回答列表
    static public function getUserAnswerList($user_id,$pageSize=5){
        $fex = C('DB_PREFIX');
        $answerObj = M("answer as a");
        $count = $answerObj->where(array('status'=>1,'user_id'=>$user_id))->count();
        $pager = new \Org\Util\Page($count, $pageSize);
        $list = $answerObj->join('left join '.$fex.'ask as ask on a.ask_id = ask.id')
            -> field('a.id,a.info,a.add_time,ask.id as ask_id,ask.title')
            -> where('a.status = 1 and a.user_id = '.$user_id)
            -> order('a.id desc')
            ->limit($pager->firstRow.','.$pager->listRows)
            -> select();
        return array('list'=>$list,'page'=>$pager->show(),'count'=>$count);
    }

    //获取资讯列表
    static public function getNewsList($pageSize=9,$where=array('a.status'=>1),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("article as a")
                ->join('left join '.$fex.'article_cate as ac on a.cate_id = ac.id')
                ->field('a.*,ac.alias,ac.name')
                ->where($where)
                ->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $askObj = M("article as a");
        $askObj->join('left join '.$fex.'article_cate as ac on a.cate_id = ac.id');
        $askObj->field('a.*,ac.alias,ac.name');
        $askObj->where($where);
        $askObj->order('a.id desc');
        if($page){
            $askObj->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $askObj->limit($pageSize);
        }
        $arr['list'] = $askObj-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取活动列表
    static public function getActivityList($pageSize=8,$where=array('a.status'=>1),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("activity as a")
                ->join('left join '.$fex.'activity_cate as ac on a.cate_id = ac.id')
                ->field('a.*,ac.alias,ac.name')
                ->where($where)
                ->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $askObj = M("article as a");
        $askObj->join('left join '.$fex.'activity_cate as ac on a.cate_id = ac.id');
        $askObj->field('a.*,ac.alias,ac.name');
        $askObj->where($where);
        $askObj->order('a.id desc');
        if($page){
            $askObj->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $askObj->limit($pageSize);
        }
        $arr['list'] = $askObj-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取分页列表
    static public function getPageList($pageSize=8,$model,$where=array('status'=>1),$page=true){
        $arr = array();
        if($page){
            $count = M($model)->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $Obj = M($model);
        $Obj->where($where);
        $Obj->order('id desc');
        if($page){
            $Obj->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $Obj->limit($pageSize);
        }
        $arr['list'] = $Obj-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取新房搜索列表
    static public function getHouseSearch($menu,$page='',$pageSize=12){
        $url = array();
        $search = array('h.status'=>1);
        $arr = array();
        $s = array();
        $url['alias'] = I('get.alias','');
        $url['metro'] = I('get.metro','');
        $url['price'] = I('get.price','');
        $url['tags'] = I('get.tags','');
        $url['roomType'] = I('get.roomType','');
        $url['houseType'] = I('get.houseType','');
        $url['houseStatus'] = I('get.houseStatus','');
        $url['renovationType'] = I('get.renovationType','');
        $url['floor'] = I('get.floor','');
        $url['group'] = I('get.group','');
        $url['key'] = I('get.key','');
        $url['sort'] = intval($_GET['sort']);
        $url = array_filter($url);
        $arr['house_cate'] = M('house_cate')->where(array('pid'=>0,'status'=>1))->order('ordid desc')->field('id,pid,name,alias')->limit(30)->select();
        if($url['alias'] && $url['alias']!='all'){
            $cate_info = M('house_cate')->where(array('alias'=>$url['alias'],'status'=>1))->find();
            if(!$cate_info['id']){
                redirect(U('House/index'));
            }
            //pid存在就是二级分类，相反一级
            if($cate_info['pid']){
                $arr['cate_pid_info'] = M('house_cate')->where(array('id'=>$cate_info['pid']))->field('id,pid,name,alias')->find();
                $arr['cate_pid_list'] = M('house_cate')->where(array('pid'=>$cate_info['pid']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $search['h.area_id'] = array('eq',$cate_info['id']);
            }else{
                $arr['cate_pid_info'] = $cate_info;
                $arr['cate_pid_list'] = M('house_cate')->where(array('pid'=>$cate_info['id']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $cate_ids = array();
                foreach($arr['cate_pid_list'] as $v){
                    $cate_ids[] = $v['id'];
                }
				  array_push($cate_ids,$cate_info['id']);
                $search['h.area_id'] = array('in',implode(',',$cate_ids));
            }
            $s['alias'] = array(
                'url'=>getUrl('House/index','alias',$url),
                'name'=>$arr['cate_pid_info']['name']
            );
        }else{
            $arr['cate_pid_list'] = $menu[8];
            $arr['metro'] = 1;
        }

        if($url['metro']){
            $search['h.metro'] = array('eq',$url['metro']);
            $s['metro'] = self::setHouseS('metro',8,$menu,$url);
        }

        if($url['price']){
            $price = explode(',',$menu[19][$url['price']]['parameter']);
            $search['h.average_price'] = array('between',array($price[0],$price[1]));
            $s['price'] = self::setHouseS('price',19,$menu,$url);
        }
        if($url['tags']){
            $search['h.tags'] = array('like','%|'.$url['tags'].'|%');
            $s['tags'] = self::setHouseS('tags',6,$menu,$url);
        }
        if($url['roomType']){
            $search['h.room_type'] = array('like','%|'.$url['roomType'].'|%');
            $s['roomType'] = self::setHouseS('roomType',3,$menu,$url);
        }
        if($url['houseType']){
            $search['h.house_type'] = array('like','%|'.$url['houseType'].'|%');
            $s['houseType'] = self::setHouseS('houseType',2,$menu,$url);
        }
        if($url['houseStatus']){
            $search['h.house_status'] = $url['houseStatus'];
            $s['houseStatus'] = self::setHouseS('houseStatus',4,$menu,$url);
        }
        if($url['renovationType']){
            $search['h.renovation_type'] = $url['renovationType'];
            $s['renovationType'] = self::setHouseS('renovationType',10,$menu,$url);
        }
        if($url['floor']){
            $search['h.floor'] = array('like','%|'.$url['floor'].'|%');
            $s['floor'] = self::setHouseS('floor',9,$menu,$url);
        }
        if($url['group']){
            $search['h.is_group'] = array('eq',1);
            $s['group'] = array(
                'url'=>getUrl('House/index','group',$url),
                'name'=>'团购特惠'
            );
        }

        if($url['key']){
            $search['h.title'] = array('like','%'.$url['key'].'%');
            $s['key'] = array(
                'url'=>getUrl('House/index','key',$url),
                'name'=>'关键词:'.$url['key']
            );
        }

        $setSort = self::setSort($url);

        if($page){
            $arr['count'] = M("house as h")->where($search)->count();
            $pager = new \Org\Util\Page($arr['count'], $pageSize);
            $arr['list'] = M("house as h")
                            ->field('h.*')
                            ->where($search)
                            ->limit($pager->firstRow.','.$pager->listRows)
                            ->order($setSort['order'])
                            ->select();
            $arr['page'] = $pager->show();
        }else{
            $arr['list'] = M("house as h")->field('h.*')->where($search)->limit($pageSize)->order($setSort['order'])->select();
        }

        return array('url'=>$url,'search'=>$search,'arr'=>$arr,'s'=>$s,'sort'=>$setSort['sort']);
    }


    //获取二手房搜索列表
    static public function getUsedSearch($menu,$page='',$pageSize=12,$action = 'Used/index'){
        $url = array();
        $search = array('h.status'=>1);
        $arr = array();
        $s = array();
        $url['alias'] = I('get.alias','');
        $url['metro'] = intval($_GET['metro']);
        $url['totalPrice'] = intval($_GET['totalPrice']);
        $url['area'] = intval($_GET['area']);
        $url['orientations'] = intval($_GET['orientations']);
        $url['usedType'] = intval($_GET['usedType']);
        $url['mating'] = intval($_GET['mating']);
        $url['renovationType'] = intval($_GET['renovationType']);
        $url['room'] = intval($_GET['room']);
        $url['hall'] = intval($_GET['hall']);
        $url['kitchen'] = intval($_GET['kitchen']);
        $url['toilet'] = intval($_GET['toilet']);
        $url['balcony'] = intval($_GET['balcony']);
        $url['key'] = I('get.key','');
        $url['other'] = $other = intval($_GET['other']);
        $url['sort'] = intval($_GET['sort']);
        $url = array_filter($url);
        $arr['area_cate'] = M('area_cate')->where(array('pid'=>0,'status'=>1))->order('ordid desc')->field('id,pid,name,alias')->limit(30)->select();
        if($url['alias'] && $url['alias']!='all'){
            $arr['cate_info'] = $cate_info = M('area_cate')->where(array('alias'=>$url['alias'],'status'=>1))->find();
            if(!$cate_info['id']){
                redirect(U($action));
            }
            //pid存在就是二级分类，相反一级
            if($cate_info['pid']){
                $arr['cate_pid_info'] = M('area_cate')->where(array('id'=>$cate_info['pid']))->field('id,pid,name,alias')->find();
                $arr['cate_pid_list'] = M('area_cate')->where(array('pid'=>$cate_info['pid']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $search['h.area_id'] = array('eq',$cate_info['id']);
            }else{
                $arr['cate_pid_info'] = $cate_info;
                $arr['cate_pid_list'] = M('area_cate')->where(array('pid'=>$cate_info['id']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $cate_ids = array();
                foreach($arr['cate_pid_list'] as $v){
                    $cate_ids[] = $v['id'];
                }
				  array_push($cate_ids,$cate_info['id']);
                $search['h.area_id'] = array('in',implode(',',$cate_ids));
            }
            $s['alias'] = array(
                'url'=>getUrl($action,'alias',$url),
                'name'=>$arr['cate_pid_info']['name']
            );
        }else{
            $arr['cate_pid_list'] = $menu[8];
            $arr['metro'] = 1;
        }

        if($url['metro']){
            $search['h.metro'] = array('eq',$url['metro']);
            $s['metro'] = self::setHouseS('metro',8,$menu,$url,$action);
        }

        if($url['totalPrice']){
            $price = explode(',',$menu[22][$url['totalPrice']]['parameter']);
            $search['h.total_price'] = array('between',array($price[0],$price[1]));
            $s['totalPrice'] = self::setHouseS('totalPrice',22,$menu,$url,$action);
        }
        if($url['area']){
            $price = explode(',',$menu[23][$url['area']]['parameter']);
            $search['h.area'] = array('between',array($price[0],$price[1]));
            $s['area'] = self::setHouseS('area',23,$menu,$url,$action);
        }
        if($url['orientations']){
            $search['h.orientations'] = $url['orientations'];
            $s['orientations'] = self::setHouseS('orientations',17,$menu,$url,$action);
        }
        if($url['usedType']){
            $search['h.used_type'] = array('like','%|'.$url['usedType'].'|%');
            $s['usedType'] = self::setHouseS('usedType',2,$menu,$url,$action);
        }
        if($url['renovationType']){
            $search['h.renovation_type'] = $url['renovationType'];
            $s['renovationType'] = self::setHouseS('renovationType',10,$menu,$url,$action);
        }
        if($url['mating']){
            $search['h.mating'] = $url['mating'];
            $s['mating'] = self::setHouseS('mating',20,$menu,$url,$action);
        }
        if($url['room']){
            $search['h.room'] = $url['room'];
            $s['room'] = array(
                'url'=>getUrl($action,'room',$url),
                'name'=>$url['room'].'室'
            );
        }
        if($url['hall']){
            $search['h.hall'] = $url['hall'];
            $s['hall'] = array(
                'url'=>getUrl($action,'hall',$url),
                'name'=>$url['hall'].'厅'
            );
        }
        if($url['kitchen']){
            $search['h.kitchen'] = $url['kitchen'];
            $s['kitchen'] = array(
                'url'=>getUrl($action,'kitchen',$url),
                'name'=>$url['kitchen'].'卫'
            );
        }
        if($url['toilet']){
            $search['h.toilet'] = $url['toilet'];
            $s['toilet'] = array(
                'url'=>getUrl($action,'toilet',$url),
                'name'=>$url['toilet'].'厨'
            );
        }
        if($url['balcony']){
            $search['h.balcony'] = $url['balcony'];
            $s['balcony'] = array(
                'url'=>getUrl($action,'balcony',$url),
                'name'=>$url['balcony'].'阳台'
            );
        }
        if($url['key']){
            $search['h.title'] = array('like','%'.$url['key'].'%');
            $s['key'] = array(
                'url'=>getUrl($action,'key',$url),
                'name'=>'关键词:'.$url['key']
            );
        }
        if($other==1){
            $search['h.is_emergency'] = 1;
        }
        if($other==2){
            $search['h.is_rec'] = 1;
        }
        if($other==3){
            $search['h.source'] = 1;
        }

        $setSort = self::setSort($url);
        $fex = C('DB_PREFIX');
        if($page){
            $arr['count'] = M("used as h")->where($search)->count();
            $pager = new \Org\Util\Page($arr['count'], $pageSize);
            $arr['list'] = M("used as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')
                ->where($search)
                ->limit($pager->firstRow.','.$pager->listRows)
                ->order($setSort['order'])
                ->select();
            $arr['page'] = $pager->show();
        }else{
            $arr['list'] = M("used as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')->where($search)->limit($pageSize)->order($setSort['order'])->select();
        }

        return array('url'=>$url,'search'=>$search,'arr'=>$arr,'s'=>$s,'sort'=>$setSort['sort']);
    }

    //针对小区搜索二手房列表
    static public function getEstateIdUsedSearch($menu,$page='',$pageSize=12,$action = 'Estate/used'){
        $url = array();
        $search = array('h.status'=>1,'h.estate_id'=>intval($_GET['id']));
        $arr = array();
        $s = array();
        $url['totalPrice'] = intval($_GET['totalPrice']);
        $url['area'] = intval($_GET['area']);
        $url['orientations'] = intval($_GET['orientations']);
        $url['mating'] = intval($_GET['mating']);
        $url['renovationType'] = intval($_GET['renovationType']);
        $url['room'] = intval($_GET['room']);
        $url['hall'] = intval($_GET['hall']);
        $url['kitchen'] = intval($_GET['kitchen']);
        $url['toilet'] = intval($_GET['toilet']);
        $url['balcony'] = intval($_GET['balcony']);
        $url['key'] = I('get.key','');
        $url['other'] = $other = intval($_GET['other']);
        $url['sort'] = intval($_GET['sort']);
        $url = array_filter($url);

        if($url['totalPrice']){
            $price = explode(',',$menu[22][$url['totalPrice']]['parameter']);
            $search['h.total_price'] = array('between',array($price[0],$price[1]));
            $s['totalPrice'] = self::setHouseS('totalPrice',22,$menu,$url,$action);
        }
        if($url['area']){
            $price = explode(',',$menu[23][$url['area']]['parameter']);
            $search['h.area'] = array('between',array($price[0],$price[1]));
            $s['area'] = self::setHouseS('area',23,$menu,$url,$action);
        }
        if($url['orientations']){
            $search['h.orientations'] = $url['orientations'];
            $s['orientations'] = self::setHouseS('orientations',17,$menu,$url,$action);
        }

        if($url['renovationType']){
            $search['h.renovation_type'] = $url['renovationType'];
            $s['renovationType'] = self::setHouseS('renovationType',10,$menu,$url,$action);
        }
        if($url['mating']){
            $search['h.mating'] = $url['mating'];
            $s['mating'] = self::setHouseS('mating',20,$menu,$url,$action);
        }
        if($url['room']){
            $search['h.room'] = $url['room'];
            $s['room'] = array(
                'url'=>getUrl($action,'room',$url),
                'name'=>$url['room'].'室'
            );
        }
        if($url['hall']){
            $search['h.hall'] = $url['hall'];
            $s['hall'] = array(
                'url'=>getUrl($action,'hall',$url),
                'name'=>$url['hall'].'厅'
            );
        }
        if($url['kitchen']){
            $search['h.kitchen'] = $url['kitchen'];
            $s['kitchen'] = array(
                'url'=>getUrl($action,'kitchen',$url),
                'name'=>$url['kitchen'].'卫'
            );
        }
        if($url['toilet']){
            $search['h.toilet'] = $url['toilet'];
            $s['toilet'] = array(
                'url'=>getUrl($action,'toilet',$url),
                'name'=>$url['toilet'].'厨'
            );
        }
        if($url['balcony']){
            $search['h.balcony'] = $url['balcony'];
            $s['balcony'] = array(
                'url'=>getUrl($action,'balcony',$url),
                'name'=>$url['balcony'].'阳台'
            );
        }
        if($url['key']){
            $search['h.title'] = array('like','%'.$url['key'].'%');
            $s['key'] = array(
                'url'=>getUrl($action,'key',$url),
                'name'=>'关键词:'.$url['key']
            );
        }
        if($other==1){
            $search['h.is_emergency'] = 1;
        }
        if($other==2){
            $search['h.is_rec'] = 1;
        }
        if($other==3){
            $search['h.source'] = 1;
        }

        $setSort = self::setSort($url);
        $fex = C('DB_PREFIX');
        if($page){
            $arr['count'] = M("used as h")->where($search)->count();
            $pager = new \Org\Util\Page($arr['count'], $pageSize);
            $arr['list'] = M("used as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')
                ->where($search)
                ->limit($pager->firstRow.','.$pager->listRows)
                ->order($setSort['order'])
                ->select();
            $arr['page'] = $pager->show();
        }else{
            $arr['list'] = M("used as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')->where($search)->limit($pageSize)->order($setSort['order'])->select();
        }

        return array('url'=>$url,'search'=>$search,'arr'=>$arr,'s'=>$s,'sort'=>$setSort['sort']);
    }

    //针对小区搜索租房列表
    static public function getEstateIdRentalSearch($menu,$page='',$pageSize=12,$action = 'Estate/rental'){
        $url = array();
        $search = array('h.status'=>1,'h.estate_id'=>intval($_GET['id']));
        $arr = array();
        $s = array();
        $url['price'] = intval($_GET['price']);
        $url['area'] = intval($_GET['area']);
        $url['orientations'] = intval($_GET['orientations']);
        $url['rentalMode'] = intval($_GET['rentalMode']);
        $url['houseType'] = intval($_GET['houseType']);
        $url['mating'] = intval($_GET['mating']);
        $url['renovationType'] = intval($_GET['renovationType']);
        $url['payType'] = intval($_GET['payType']);
        $url['room'] = intval($_GET['room']);
        $url['hall'] = intval($_GET['hall']);
        $url['kitchen'] = intval($_GET['kitchen']);
        $url['toilet'] = intval($_GET['toilet']);
        $url['balcony'] = intval($_GET['balcony']);
        $url['key'] = I('get.key','');
        $url['other'] = $other = intval($_GET['other']);
        $url['sort'] = intval($_GET['sort']);
        $url = array_filter($url);

        if($url['price']){
            $price = explode(',',$menu[27][$url['price']]['parameter']);
            $search['h.price'] = array('between',array($price[0],$price[1]));
            $s['price'] = self::setHouseS('price',27,$menu,$url,$action);
        }
        if($url['area']){
            $price = explode(',',$menu[23][$url['area']]['parameter']);
            $search['h.area'] = array('between',array($price[0],$price[1]));
            $s['area'] = self::setHouseS('area',23,$menu,$url,$action);
        }
        if($url['orientations']){
            $search['h.orientations'] = $url['orientations'];
            $s['orientations'] = self::setHouseS('orientations',17,$menu,$url,$action);
        }
        if($url['rentalMode']){
            $search['h.rental_mode'] = $url['rentalMode'];
            $s['rentalMode'] = self::setHouseS('rentalMode',26,$menu,$url,$action);
        }
        if($url['houseType']){
            $search['h.house_type'] = array('like','%|'.$url['houseType'].'|%');
            $s['houseType'] = self::setHouseS('houseType',2,$menu,$url,$action);
        }
        if($url['renovationType']){
            $search['h.renovation_type'] = $url['renovationType'];
            $s['renovationType'] = self::setHouseS('renovationType',10,$menu,$url,$action);
        }
        if($url['mating']){
            $search['h.mating'] = $url['mating'];
            $s['mating'] = self::setHouseS('mating',20,$menu,$url,$action);
        }
        if($url['room']){
            $search['h.room'] = $url['room'];
            $s['room'] = array(
                'url'=>getUrl($action,'room',$url),
                'name'=>$url['room'].'室'
            );
        }
        if($url['hall']){
            $search['h.hall'] = $url['hall'];
            $s['hall'] = array(
                'url'=>getUrl($action,'hall',$url),
                'name'=>$url['hall'].'厅'
            );
        }
        if($url['kitchen']){
            $search['h.kitchen'] = $url['kitchen'];
            $s['kitchen'] = array(
                'url'=>getUrl($action,'kitchen',$url),
                'name'=>$url['kitchen'].'卫'
            );
        }
        if($url['toilet']){
            $search['h.toilet'] = $url['toilet'];
            $s['toilet'] = array(
                'url'=>getUrl($action,'toilet',$url),
                'name'=>$url['toilet'].'厨'
            );
        }
        if($url['balcony']){
            $search['h.balcony'] = $url['balcony'];
            $s['balcony'] = array(
                'url'=>getUrl($action,'balcony',$url),
                'name'=>$url['balcony'].'阳台'
            );
        }
        if($url['payType']){
            $search['h.pay_type'] = $url['payType'];
            $s['payType'] = self::setHouseS('payType',25,$menu,$url,$action);
        }
        if($url['key']){
            $search['h.title'] = array('like','%'.$url['key'].'%');
            $s['key'] = array(
                'url'=>getUrl($action,'key',$url),
                'name'=>'关键词:'.$url['key']
            );
        }

        if($other==1){
            $search['h.is_emergency'] = 1;
        }
        if($other==2){
            $search['h.is_rec'] = 1;
        }
        if($other==3){
            $search['h.source'] = 1;
        }

        $setSort = self::setSort($url);
        $fex = C('DB_PREFIX');
        if($page){
            $arr['count'] = M("rental as h")->where($search)->count();
            $pager = new \Org\Util\Page($arr['count'], $pageSize);
            $arr['list'] = M("rental as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')
                ->where($search)
                ->limit($pager->firstRow.','.$pager->listRows)
                ->order($setSort['order'])
                ->select();
            $arr['page'] = $pager->show();
        }else{
            $arr['list'] = M("rental as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')->where($search)->limit($pageSize)->order($setSort['order'])->select();
        }

        return array('url'=>$url,'search'=>$search,'arr'=>$arr,'s'=>$s,'sort'=>$setSort['sort']);
    }

    //获取租房搜索列表
    static public function getRentalSearch($menu,$page='',$pageSize=12){
        $url = array();
        $search = array('h.status'=>1);
        $arr = array();
        $s = array();
        $action = 'Rental/index';
        $url['alias'] = I('get.alias','');
        $url['metro'] = intval($_GET['metro']);
        $url['price'] = intval($_GET['price']);
        $url['area'] = intval($_GET['area']);
        $url['orientations'] = intval($_GET['orientations']);
        $url['rentalMode'] = intval($_GET['rentalMode']);
        $url['houseType'] = intval($_GET['houseType']);
        $url['mating'] = intval($_GET['mating']);
        $url['renovationType'] = intval($_GET['renovationType']);
        $url['payType'] = intval($_GET['payType']);
        $url['room'] = intval($_GET['room']);
        $url['hall'] = intval($_GET['hall']);
        $url['kitchen'] = intval($_GET['kitchen']);
        $url['toilet'] = intval($_GET['toilet']);
        $url['balcony'] = intval($_GET['balcony']);
        $url['key'] = I('get.key','');
        $url['other'] = $other = intval($_GET['other']);
        $url['sort'] = intval($_GET['sort']);
        $url = array_filter($url);
        $arr['area_cate'] = M('area_cate')->where(array('pid'=>0,'status'=>1))->order('ordid desc')->field('id,pid,name,alias')->limit(30)->select();
        if($url['alias'] && $url['alias']!='all'){
            $arr['cate_info'] = $cate_info = M('area_cate')->where(array('alias'=>$url['alias'],'status'=>1))->find();
            if(!$cate_info['id']){
                redirect(U('Rental/index'));
            }
            //pid存在就是二级分类，相反一级
            if($cate_info['pid']){
                $arr['cate_pid_info'] = M('area_cate')->where(array('id'=>$cate_info['pid']))->field('id,pid,name,alias')->find();
                $arr['cate_pid_list'] = M('area_cate')->where(array('pid'=>$cate_info['pid']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $search['h.area_id'] = array('eq',$cate_info['id']);
            }else{
                $arr['cate_pid_info'] = $cate_info;
                $arr['cate_pid_list'] = M('area_cate')->where(array('pid'=>$cate_info['id']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $cate_ids = array();
                foreach($arr['cate_pid_list'] as $v){
                    $cate_ids[] = $v['id'];
                }
				  array_push($cate_ids,$cate_info['id']);
                $search['h.area_id'] = array('in',implode(',',$cate_ids));
            }
            $s['alias'] = array(
                'url'=>getUrl('Rental/index','alias',$url),
                'name'=>$arr['cate_pid_info']['name']
            );
        }else{
            $arr['cate_pid_list'] = $menu[8];
            $arr['metro'] = 1;
        }

        if($url['metro']){
            $search['h.metro'] = array('eq',$url['metro']);
            $s['metro'] = self::setHouseS('metro',8,$menu,$url,$action);
        }

        if($url['price']){
            $price = explode(',',$menu[27][$url['price']]['parameter']);
            $search['h.price'] = array('between',array($price[0],$price[1]));
            $s['price'] = self::setHouseS('price',27,$menu,$url,$action);
        }
        if($url['area']){
            $price = explode(',',$menu[23][$url['area']]['parameter']);
            $search['h.area'] = array('between',array($price[0],$price[1]));
            $s['area'] = self::setHouseS('area',23,$menu,$url,$action);
          //  p($search['h.area']);
        }
        if($url['orientations']){
            $search['h.orientations'] = $url['orientations'];
            $s['orientations'] = self::setHouseS('orientations',17,$menu,$url,$action);
        }
        if($url['rentalMode']){
            $search['h.rental_mode'] = $url['rentalMode'];
            $s['rentalMode'] = self::setHouseS('rentalMode',26,$menu,$url,$action);
        }
        if($url['houseType']){
            $search['h.house_type'] = array('like','%|'.$url['houseType'].'|%');
            $s['houseType'] = self::setHouseS('houseType',2,$menu,$url,$action);
        }
        if($url['renovationType']){
            $search['h.renovation_type'] = $url['renovationType'];
            $s['renovationType'] = self::setHouseS('renovationType',10,$menu,$url,$action);
        }
        if($url['mating']){
            $search['h.mating'] = $url['mating'];
            $s['mating'] = self::setHouseS('mating',20,$menu,$url,$action);
        }
        if($url['room']){
            $search['h.room'] = $url['room'];
            $s['room'] = array(
                'url'=>getUrl('Rental/index','room',$url),
                'name'=>$url['room'].'室'
            );
        }
        if($url['hall']){
            $search['h.hall'] = $url['hall'];
            $s['hall'] = array(
                'url'=>getUrl('Rental/index','hall',$url),
                'name'=>$url['hall'].'厅'
            );
        }
        if($url['kitchen']){
            $search['h.kitchen'] = $url['kitchen'];
            $s['kitchen'] = array(
                'url'=>getUrl('Rental/index','kitchen',$url),
                'name'=>$url['kitchen'].'卫'
            );
        }
        if($url['toilet']){
            $search['h.toilet'] = $url['toilet'];
            $s['toilet'] = array(
                'url'=>getUrl('Rental/index','toilet',$url),
                'name'=>$url['toilet'].'厨'
            );
        }
        if($url['balcony']){
            $search['h.balcony'] = $url['balcony'];
            $s['balcony'] = array(
                'url'=>getUrl('Rental/index','balcony',$url),
                'name'=>$url['balcony'].'阳台'
            );
        }
        if($url['payType']){
            $search['h.pay_type'] = $url['payType'];
            $s['payType'] = self::setHouseS('payType',25,$menu,$url,$action);
        }
        if($url['key']){
            $search['h.title'] = array('like','%'.$url['key'].'%');
            $s['key'] = array(
                'url'=>getUrl('Rental/index','key',$url),
                'name'=>'关键词:'.$url['key']
            );
        }

        if($other==1){
            $search['h.is_emergency'] = 1;
        }
        if($other==2){
            $search['h.is_rec'] = 1;
        }
        if($other==3){
            $search['h.source'] = 1;
        }

        $setSort = self::setSort($url);
        $fex = C('DB_PREFIX');
        if($page){
            $arr['count'] = M("rental as h")->where($search)->count();
            $pager = new \Org\Util\Page($arr['count'], $pageSize);
            $arr['list'] = M("rental as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')
                ->where($search)
                ->limit($pager->firstRow.','.$pager->listRows)
                ->order($setSort['order'])
                ->select();
            $arr['page'] = $pager->show();
        }else{
            $arr['list'] = M("rental as h")->join('left join '.$fex.'user_info as ui on h.user_id = ui.user_id')
                ->field('h.*,ui.head_ico,ui.true_name,ui.housing')->where($search)->limit($pageSize)->order($setSort['order'])->select();
        }

        return array('url'=>$url,'search'=>$search,'arr'=>$arr,'s'=>$s,'sort'=>$setSort['sort']);
    }

    //获取小区搜索列表
    static public function getEstateSearch($menu,$page='',$pageSize=12){
        $url = array();
        $search = array('h.status'=>1);
        $arr = array();
        $s = array();
        $action = 'Estate/index';
        $url['alias'] = I('get.alias','');
        $url['metro'] = intval($_GET['metro']);
        $url['houseType'] = intval($_GET['houseType']);
        $url['years'] = intval($_GET['years']);
        $url['key'] = I('get.key','');
        $url['sort'] = intval($_GET['sort']);
        $url = array_filter($url);
        $arr['area_cate'] = M('area_cate')->where(array('pid'=>0,'status'=>1))->order('ordid desc')->field('id,pid,name,alias')->limit(30)->select();
        if($url['alias'] && $url['alias']!='all'){
            $arr['cate_info'] = $cate_info = M('area_cate')->where(array('alias'=>$url['alias'],'status'=>1))->find();
            if(!$cate_info['id']){
                redirect(U('Estate/index'));
            }
            //pid存在就是二级分类，相反一级
            if($cate_info['pid']){
                $arr['cate_pid_info'] = M('area_cate')->where(array('id'=>$cate_info['pid']))->field('id,pid,name,alias')->find();
                $arr['cate_pid_list'] = M('area_cate')->where(array('pid'=>$cate_info['pid']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $search['h.area_id'] = array('eq',$cate_info['id']);
            }else{
                $arr['cate_pid_info'] = $cate_info;
                $arr['cate_pid_list'] = M('area_cate')->where(array('pid'=>$cate_info['id']))->field('id,pid,name,alias')->order('ordid desc')->select();
                $cate_ids = array();
                foreach($arr['cate_pid_list'] as $v){
                    $cate_ids[] = $v['id'];
                }
				  array_push($cate_ids,$cate_info['id']);
                $search['h.area_id'] = array('in',implode(',',$cate_ids));
            }
            $s['alias'] = array(
                'url'=>getUrl('Estate/index','alias',$url),
                'name'=>$arr['cate_pid_info']['name']
            );
        }else{
            $arr['cate_pid_list'] = $menu[8];
            $arr['metro'] = 1;
        }

        if($url['metro']){
            $search['h.metro'] = array('eq',$url['metro']);
            $s['metro'] = self::setHouseS('metro',8,$menu,$url,$action);
        }

        if($url['years']){
            $years = explode(',',$menu[21][$url['years']]['parameter']);
            $y = date('Y');
            $search['h.years'] = array('between',array($y-$years[1],$y-$years[0]));
            $s['years'] = self::setHouseS('years',21,$menu,$url,$action);
        }

        if($url['houseType']){
            $search['h.house_type'] = array('like','%|'.$url['houseType'].'|%');
            $s['houseType'] = self::setHouseS('houseType',2,$menu,$url,$action);
        }

        if($url['key']){
            $search['h.title'] = array('like','%'.$url['key'].'%');
            $s['key'] = array(
                'url'=>getUrl('Estate/index','key',$url),
                'name'=>'关键词:'.$url['key']
            );
        }

        $setSort = self::setSort($url);
        if($page){
            $arr['count'] = M("estate as h")->where($search)->count();
            $pager = new \Org\Util\Page($arr['count'], $pageSize);
            $arr['list'] = M("estate as h")
                ->field('h.*')
                ->where($search)
                ->limit($pager->firstRow.','.$pager->listRows)
                ->order($setSort['order'])
                ->select();
            $arr['page'] = $pager->show();
        }else{
            $arr['list'] = M("estate as h")->field('h.*')->where($search)->limit($pageSize)->order($setSort['order'])->select();
        }

        return array('url'=>$url,'search'=>$search,'arr'=>$arr,'s'=>$s,'sort'=>$setSort['sort']);
    }

    private function setHouseS($name,$menu_id,$menu,$url,$action='House/index'){
        return  array(
            'url'=>getUrl($action,$name,$url),
            'name'=>$menu[$menu_id][$url[$name]]['name']
        );
    }

    private function setSort($url){
        $sort = array('1'=>1,'2'=>3,'3'=>5,'4'=>8);
        $order = 'h.id desc';
        if($url['sort']){
            if($url['sort']==1){
                $order = 'h.id desc';
                $sort[1] = 2;
            }
            if($url['sort']==2){
                $order = 'h.id asc';
                $sort[1] = 1;
            }
            if($url['sort']==3){
                $order = 'h.average_price asc';
                $sort[2] = 4;
            }
            if($url['sort']==4){
                $order = 'h.average_price desc';
                $sort[2] = 3;
            }
            if($url['sort']==5){
                $order = 'h.total_price asc';
                $sort[3] = 6;
            }
            if($url['sort']==6){
                $order = 'h.total_price desc';
                $sort[3] = 5;
            }
            if($url['sort']==8){
                $order = 'h.price desc';
                $sort[4] = 9;
            }
            if($url['sort']==9){
                $order = 'h.price desc';
                $sort[4] = 8;
            }
        }
        return array('order'=>$order,'sort'=>$sort);
    }

    static public function getRecHouse($limit=8){
        $list =  M('house')->where(array('is_rec'=>1,'status'=>1))->order('ordid desc')->limit($limit)->select();
        return $list;
    }

    static public function getRecUsed($limit=8){
        $list =  M('used')->where(array('is_rec'=>1,'status'=>1))->order('ordid desc')->limit($limit)->select();
        return $list;
    }

    static public function getRecRental($limit=8){
        $list =  M('rental')->where(array('is_rec'=>1,'status'=>1))->order('ordid desc')->limit($limit)->select();
        return $list;
    }

    //获取热门楼盘
    static public function getHitsHouse($limit=1,$where = array('h.status'=>1)){
        $fex = C('DB_PREFIX');
        $list = M("house as h")->join('left join '.$fex.'house_info as hi on h.id = hi.house_id')
            ->field('h.id,h.title')
            ->where($where)
            ->order('hi.hits desc')
            ->limit($limit)
            ->select();
        return $list;
    }

}

?>
